1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Text;
7 using System.Windows.Forms;
8
9 namespace pong
10 {
11 public partial class Form1 : Form
12 {
13 //Vars
14 int xspeed;
15 int yspeed;
16 int lastx;
17 int lastx_cpu;
18 int score_player;
19 int score_cpu;
20 int topBounds;
21 int bottomBounds;
22 int leftBounds;
23 int rightBounds;
24 bool paused = false;
25
26 public Form1()
27 {
28 InitializeComponent();
29
30 //Set up all the initial speed
31 xspeed = 2;
32 yspeed = 2;
33
34 //Make the buttons no longer clickable
35 ball.Enabled = false;
36 paddle.Enabled = false;
37
38 //Last mouse X position stored here (so we can add curve based on how fast the mouse was moved)
39 lastx = MousePosition.X;
40 lastx_cpu = paddle2.Location.X;
41
42 //Scores
43 score_player = 0;
44 score_cpu = 0;
45
46 //Screen Boundaries
47 topBounds = 0;
48 bottomBounds = this.Height;
49 leftBounds = 0;
50 rightBounds = this.Width;
51
52 //Hide the pause text (since it defaults on)
53 pause_txt.Visible = false;
54
55 //Double Buffer (without this technique the screen will flash)
56 this.SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
57 }
58
59 //Moves the ball each frame
60 private void moveBall(object sender, EventArgs e)
61 {
62 //Adjusting the bounds (a bit sloppy but useful for testing)
63 topBounds = 0;
64 bottomBounds = this.Height-23;
65 leftBounds = 0;
66 rightBounds = this.Width;
67
68 //If not paused we can advance the position of everything
69 if (!paused)
70 {
71 //Player 1
72 paddle.Location = new Point((int)(MousePosition.X - paddle.Width), paddle.Location.Y);
73 ball.Location = new Point(ball.Location.X + xspeed, ball.Location.Y + yspeed);
74
75 //Computer
76 if (ball.Location.X > paddle2.Location.X)
77 {
78 paddle2.Location = new Point(paddle2.Location.X + 3, paddle2.Location.Y);
79 }
80 else
81 {
82 paddle2.Location = new Point(paddle2.Location.X - 3, paddle2.Location.Y);
83 }
84
85 //Ball Control: Left Wall
86 if (ball.Location.X < leftBounds)
87 {
88 xspeed *= -1;
89 while (ball.Location.X - 1 < leftBounds)
90 {
91 ball.Location = new Point(ball.Location.X + 1, ball.Location.Y);
92 }
93 }
94
95 //Ball Control: Right Wall
96 if (ball.Location.X + ball.Width > rightBounds)
97 {
98 xspeed *= -1;
99 while (ball.Location.X + 1 > rightBounds)
100 {
101 ball.Location = new Point(ball.Location.X - 1, ball.Location.Y);
102 }
103 }
104
105 //Ball Control: Player Paddle
106 if (ball.Location.Y + ball.Height > paddle.Location.Y && ball.Location.X > (int)(paddle.Location.X - ball.Width / 2) && ball.Location.X + ball.Width < (int)(paddle.Location.X + paddle.Width + ball.Width / 2) && ball.Location.Y < (int)(paddle.Location.Y + paddle.Height / 2))
107 {
108 yspeed *= -1;
109 xspeed = Math.Abs(MousePosition.X - lastx);
110 if (xspeed > 4)
111 {
112 xspeed = 4;
113 }
114 else if (xspeed < -4)
115 {
116 xspeed = -4;
117 }
118 else if (xspeed == 0)
119 {
120 Random a = new Random();
121 if (a.NextDouble() > .5)
122 {
123 xspeed = 2;
124 }
125 else
126 {
127 xspeed = -2;
128 }
129 }
130 while (ball.Location.Y + 1 + ball.Height > paddle.Location.Y)
131 {
132 ball.Location = new Point(ball.Location.X, ball.Location.Y - 1);
133 }
134 }
135
136 //Ball Control: CPU Paddle
137 if (ball.Location.Y < paddle2.Location.Y + paddle2.Height && ball.Location.X > (int)(paddle2.Location.X - ball.Width / 2) && ball.Location.X + ball.Width < (int)(paddle2.Location.X + paddle.Width + ball.Width / 2) && ball.Location.Y > (int)(paddle2.Location.Y + paddle2.Height / 2))
138 {
139 yspeed *= -1;
140 xspeed = Math.Abs(paddle.Location.X - lastx_cpu);
141 if (xspeed > 4)
142 {
143 xspeed = 4;
144 }
145 else if (xspeed < -4)
146 {
147 xspeed = -4;
148 }
149 else if (xspeed == 0)
150 {
151 Random a = new Random();
152 if (a.NextDouble() > .5)
153 {
154 xspeed = 2;
155 }
156 else
157 {
158 xspeed = -2;
159 }
160 }
161 while (ball.Location.Y - 1 < paddle2.Location.Y + paddle2.Height)
162 {
163 ball.Location = new Point(ball.Location.X, ball.Location.Y + 1);
164 }
165 }
166
167 //Ball Control: CPU Scoring
168 if (ball.Location.Y > bottomBounds)
169 {
170 ball.Location = new Point(120, 100);
171 Random b = new Random();
172 if (b.NextDouble() > .5)
173 {
174 xspeed = 2;
175 }
176 else
177 {
178 xspeed = -2;
179 }
180 yspeed = -2;
181 score_cpu++;
182 points2.Text = "CPU: " + score_cpu;
183 } //Ball Control - Player Scoring
184 else if (ball.Location.Y < topBounds)
185 {
186 ball.Location = new Point(120, 100);
187 Random b = new Random();
188 if (b.NextDouble() > .5)
189 {
190 xspeed = 2;
191 }
192 else
193 {
194 xspeed = -2;
195 }
196 yspeed = 2;
197 score_player++;
198 points1.Text = "Player: " + score_player;
199 }
200 lastx = MousePosition.X;
201 lastx_cpu = paddle2.Location.X;
202 }
203 }
204
205
206 private void movePaddles(object sender, EventArgs e)
207 {
208 //Properly position the paddles
209 paddle.Location = new Point(paddle.Location.X, bottomBounds - 46);
210 paddle2.Location = new Point(paddle2.Location.X,topBounds + 12);
211 pause_txt.Location = new Point((int)rightBounds / 2 - pause_txt.Width / 2, (int)bottomBounds / 2 - pause_txt.Height / 2);
212 }
213
214 private void pause(object sender, EventArgs e)
215 {
216 //Toggle pausing by clicking
217 if (!paused)
218 {
219 paused = true;
220 pause_txt.Visible = true;
221 }
222 else
223 {
224 paused = false;
225 pause_txt.Visible = false;
226 }
227 }
228
229 //Double Buffer (required function)
230 protected override void OnPaint(PaintEventArgs pe)
231 {
232
233 }
234 }
235 }
Vars
Set up all the initial speed
Make the buttons no longer clickable
Last mouse X position stored here (so we can add curve based on how fast the mouse was moved)
Scores
Screen Boundaries
Hide the pause text (since it defaults on)
Double Buffer (without this technique the screen will flash)
Moves the ball each frame
Adjusting the bounds (a bit sloppy but useful for testing)
If not paused we can advance the position of everything
Player 1
Computer
Ball Control: Left Wall
Ball Control: Right Wall
Ball Control: Player Paddle
Ball Control: CPU Paddle
Ball Control: CPU Scoring
} Ball Control - Player Scoring
Properly position the paddles
Toggle pausing by clicking
Double Buffer (required function)